<?php
declare (strict_types=1);

namespace app\backend\controller\v1;

use app\common\model\Search as SearchModel;

class Search extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $model = new SearchModel();
        $page = input('page/d', 1);
        $size = 10;
        $datas = $model
            ->order('id desc')
            ->page($page, $size)
            ->select();
        $rows = count($model->select()->toArray());
        $maxPage = ceil($rows / $size);
        return sucessMsg(200, '获取成功！', ['data' => $datas, 'page' => $page, 'maxPage' => $maxPage, 'rows' => $rows]);
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            return errMsg(422, 'id不能为空');
        }
        $data = SearchModel::where('id', $id)->find();
        if (empty($data))
            return errMsg(422, '无效id');
        if (SearchModel::destroy($id))
            return sucessMsg(200, '删除成功');
        return errMsg(422, '删除失败');
    }
}

